package com.sourceQ.system.service.message.mail.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sourceQ.system.domain.message.mail.SysMailAccount;
import com.sourceQ.system.dto.message.MailAccountPageReqDTO;
import com.sourceQ.system.dto.message.MailAccountSaveReqDTO;
import com.sourceQ.system.mapper.message.mail.SysMailAccountMapper;
import com.sourceQ.system.service.message.mail.SysMailAccountService;
import com.sourceQ.system.service.message.mail.SysMailTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.sourceQ.common.core.constant.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS;
import static com.sourceQ.common.core.constant.ErrorCodeConstants.MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS;
import static com.sourceQ.common.core.utils.ExceptionUtil.exception;

/**
 * @author: SourceQ
 * @create: 2024-04-18 19:25
 * @Description:
 */
@Service
public class SysMailAccountServiceImpl extends ServiceImpl<SysMailAccountMapper, SysMailAccount> implements SysMailAccountService {

    @Autowired
    private SysMailTemplateService mailTemplateService;
    @Override
    public Long createMailAccount(MailAccountSaveReqDTO createReqVO) {
        SysMailAccount account = new SysMailAccount();
        BeanUtils.copyProperties(createReqVO,account);
        baseMapper.insert(account);
        return account.getId();
    }

    @Override
    public void updateMailAccount(MailAccountSaveReqDTO updateReqVO) {
        SysMailAccount account = validateMailAccountExists(updateReqVO.getId());
        BeanUtils.copyProperties(updateReqVO,account);
        updateById(account);
    }

    @Override
    public void deleteMailAccount(Long id) {
        validateMailAccountExists(id);
        // 校验是否存在关联模版
        if (mailTemplateService.getMailTemplateCountByAccountId(id) > 0) {
            throw exception(MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS);
        }
        baseMapper.deleteById(id);
    }

    @Override
    public SysMailAccount getMailAccount(Long id) {
        return validateMailAccountExists(id);
    }

    @Override
    public SysMailAccount getMailAccountFromCache(Long id) {
        return getMailAccount(id);
    }

    @Override
    public IPage<SysMailAccount> getMailAccountPage(Page page, MailAccountPageReqDTO pageReqVO) {
        return baseMapper.getMailAccountPage(page,pageReqVO);
    }

    @Override
    public List<SysMailAccount> getMailAccountList() {
        return baseMapper.selectList(Wrappers.<SysMailAccount>lambdaQuery()
                .eq(SysMailAccount::getDelFlag,"0"));
    }

    /**
     * 校验数据是否存在
     * @param id
     */
    private SysMailAccount validateMailAccountExists(Long id) {
        SysMailAccount account = baseMapper.selectById(id);
        if (account == null) {
            throw exception(MAIL_ACCOUNT_NOT_EXISTS);
        }
        return account;
    }
}
